<?php
class Application_Model_DbTable_Advertisement extends Zend_Db_Table_Abstract{
	//ten bang
	protected $_name = "tbl_advertisement";
	
	//khoa chinh
	protected $_primary = 'advertisement_id';
	
	protected $_referenceMap = array (
			'tbl_advertisement_category' => array (
					'columns' => array('advertisement_advertisement_category_id'),
					'refTableClass' => 'Application_Model_DbTable_AdvertisementCategory'
			),
	);
	
	public function getList($filters = array(),$sortType = "ASC",$sortBy = null){
		
		$select = $this->select()->setIntegrityCheck(false)
		->from($this->_name);	
					
		if( count($filters) > 0 ){			
			foreach($filters as $key => $value){
				
				if($key=='id'){
					$select->where('advertisement_advertisement_category_id = ?',$value);
				}
				if($key=="keyword" && trim($value)!=""){
					$select->where('advertisement_name like ?',"%$value%");
				}
				
				if($key=="fromDate" && $value!=null){
					$select->where('advertisement_creation_time >= ?',$value);
				}
				
				if($key=="toDate" && $value!=null){
					$select->where('advertisement_creation_time <= ?',$value);
				}
				
			}//foreach
		}//end if
		
		if($sortBy==null){
			$sortBy = "advertisement_creation_time";
			$sortType = "Desc";
		}
		
		//sql
		$select->order($sortBy . " " . $sortType);	
			 
		return $this->fetchAll($select);
	}	
}